package com.itheima.demo06Optional;

import org.junit.Test;

import java.util.Optional;

public class Demo01 {

    @Test
    public void test05() {
        User u = new User("Hello", 18);
        // getUpperUserName1(u);
        Optional<User> op = Optional.of(u);
        String str = op.map(User::getUserName).map(String::toUpperCase).orElse("null");
        System.out.println(str);
    }

    @Test
    public void test04() {
        Optional<String> userNameO = Optional.of("凤姐");
        Optional<String> userName1 = Optional.empty();
        // ifPresent: 如果有值就调用参数
        userNameO.ifPresent(System.out::println);
        userName1.ifPresent(System.out::println);

        // ifPresentOrElse: 存在做的什么,不存在做点什么
        userNameO.ifPresent(s -> {
            System.out.println("有值: " + s);
        });

        if(userName1.isPresent()){
            System.out.println("没有值");
        }
    }

    @Test
    public void test03() {
        // Optional<String> userNameO = Optional.of("凤姐");
        Optional<String> userNameO = Optional.empty();

        // orElse: 如果Optional中有值,就取出这个值,如果没有值就使用参数指定的值
        String name = userNameO.orElse("如花吗?");
        System.out.println("name = " + name);


        Optional<String> ss = Optional.of("");
        ss.ifPresent(
                s -> System.out.println(s+"1")
        );
    }


}
